<template>
	<view class="content">
		<!-- <button @click="aaa">查看数据</button> -->
		<uni-nav-bar v-if="navBarShowTag">
			<view class="tabs-box">
				<view class="one-nav" :class="currentSwiperIndex === 0 ? 'nav-actived':''" @tap="swiperChange(0)">推荐</view>
				<view class="one-nav" :class="currentSwiperIndex === 1 ? 'nav-actived':''" @tap="swiperChange(1)">资讯</view>
			</view>
		</uni-nav-bar>
				
		<view class="header-box">
			<!-- 顶部广告位轮播图 -->
			<swiper class="swipe·r" :indicator-dots="false" :autoplay="true" :interval="2500" :duration="500">
				<swiper-item>
					<navigator open-type="navigate" url="/pages/webview/webview?url=http://baidu.com">
						<image class="banner-swiper-img"
							src="https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00140-2668.jpg"
							mode="aspectFill" />
					</navigator>
				</swiper-item>
				<swiper-item>
					<navigator open-type="navigate" url="http://baidu.com">
						<image class="banner-swiper-img"
							src="https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00140-2668.jpg"
							mode="aspectFill" />
					</navigator>
				</swiper-item>
			</swiper>
			<!-- 遮罩使用弧形框 -->
			<image class="crile" src="@/static/crile.png" mode="aspectFill" />
			<!-- 两个选项按钮 -->
			<view class="card-header">
				<view class="card-one card-left" @tap="gotoTab('/pages/feeds/feeds')">
					<image class="img" src="@/static/coffee.png" mode="aspectFill" />
					<view class="iright">
						<view class="title">精彩动态</view>
					</view>
				</view>
				<view class="card-one card-right" @tap="gotoTab('/pages/me/me')">
					<image class="img" src="@/static/ran.png" mode="aspectFill" />
					<view class="iright">
						<view class="title">个人中心</view>
					</view>
				</view>
			</view>
			<!-- Tab 选项卡 -->
			<view class="tabs-box">                                                   <!-- @tap是手指触摸离开时触发 -->        
				<view class="one-nav" :class="currentSwiperIndex === 0 ? 'nav-actived':''" @tap="swiperChange(0)">推荐
				</view>
				<view class="one-nav" :class="currentSwiperIndex === 1 ? 'nav-actived':''" @tap="swiperChange(1)">资讯
				</view>
			</view>
		</view>
		<!-- <u-button type="info">信息按钮</u-button> -->
		<!-- 内容轮播导航实现 --> <!--:style 动态设置轮播图组件高度-->
		<swiper class="swiper-box" :style=" 'height:'+ swiperSliderHeight" :current="currentSwiperIndex" @animationfinish="swiperSlider">
			<!-- 推荐动态实现 -->
			<swiper-item class="swiper-item sns-now">
				<!-- css3设置瀑布流： style="column-gap: 10px;column-count: 2;" -->
				<view class="feeds-box">
					<sns-waterfall v-model="feedsList" ref="waterfall">
						<template v-slot:left="{leftList}">
							<view class="feed-one" v-for="(item, index) in leftList" :key="index">
								<navigator open-type="navigate" @click="ab(item)" :url="'/subpages/feedinfo?obj='+ obj">
									<image class="feed-img" :src="item.img" mode="widthFix" />
									<view class="u-line-2 feed-title">
										{{item.name}}
									</view>
									<view class="feed-info">
										<view class="iview">
											<image class="avatar" :src="item.img_portrait" />
											<text class="name u-line-1">{{item.name}}</text>
										</view>
										<view class="iview">
											<view class="ilike">
												<image src="@/static/lover.png" class="micon" />
												<image src="@/static/love.png" class="micon" />
												<text class="love-count">6</text>
											</view>
										</view>
									</view>
								</navigator>
							</view>
						</template>
						<template v-slot:right="{rightList}">
							<view class="feed-one" v-for="(item, index) in rightList" :key="index">
								<navigator open-type="navigate" @click="ab(item)" :url="'/subpages/feedinfo?obj='+ obj">
									<image class="feed-img" :src="item.img" mode="widthFix" />
									<view class="u-line-2 feed-title">
										{{item.name}}
									</view>
									<view class="feed-info">
										<view class="iview">
											<image class="avatar" :src="item.img_portrait" />
											<text class="name u-line-1">{{item.name}}</text>
										</view>
										<view class="iview">
											<view class="ilike">
												<image src="@/static/lover.png" class="micon" />
												<image src="@/static/love.png" class="micon" />
												<text class="love-count">6</text>
											</view>
										</view>
									</view>
								</navigator>
							</view>
						</template>
					</sns-waterfall>
				</view>
			</swiper-item>
			<!-- 资讯列表实现 -->
			<swiper-item class="swiper-item sns-news">
				<view v-for="(item,index) in newsList" :key="index">
					<navigator class="one-new" open-type="navigate" :url=" '/subpages/newinfo?id='+item.id">
						<view class="left">
							<view class="title u-line-2">{{item.title}}</view>
							<view class="uinfo">
								<view class="iview">
									<view class="utime">
										<text class="name">{{item.txt}}</text>
									</view>
								</view>
								<text class="uptime">{{item.date_time}}发布</text>
							</view>
						</view>
						<view class="right">
							<image class="pic" mode="aspectFill" :src="item.img_portrait" />
						</view>
					</navigator>
				</view>
			</swiper-item>
		</swiper>
	
	    <login ref="login" />
	</view>
</template> 

<script>
	//给插件添加功能，并导入
	import snsWaterfall from '@/components/waterfall/sns-waterfall.vue';
	export default {
		data() {
			return {
				title: 'Hello',
				// 当前 推荐 资讯 滑动位置.
				currentSwiperIndex: 0,
				// 推荐页面数据
				feedsList: [],
				// 资讯页面的信息
				newsList: [],
				// 滑动页面轮播图的高度
				swiperSliderHeight: "1000px",
				// 推荐轮播器高度
				swiperSliderFeedsHeight: '0px',
				// 资讯轮播器高度
				swiperSliderNewsHeight: '0px',
				//导航栏的显示和隐藏
				navBarShowTag: false,
				// 记录 推荐滚动 所在的位置
				oldFeedsScrollTop: 0,
				// 记录 资讯滚动 所在位置
				oldNewsScrollTop: 0 ,
				// 给详情页面的数据
				objj:'首页',
				obj:'首页',
			}
		},
		onLoad() {
			this.adver()
		},
		components:{
			snsWaterfall
		},
		// 监听顶部用户下拉动作。下拉更新数据，
		onPullDownRefresh() {
			this.adver()
			// console.log('下拉属性：',this.$refs.waterfall);
			// if(this.currentSwiperIndex === 0){
				//下拉时，先把数据清空，在请求
				this.$refs.waterfall.clear()
				// this.adver()
			// }
			
		},
		//监听下拉置地请求的数据
		onReachBottom(){
			this.adver() //注意：这个是追加请求
		},
		//监听页面滚动事件。形参obj 自带的
		onPageScroll(obj){  
			console.log('滚动',obj);
			// 表示在推荐还是资讯页面，把两个页面的滚动距离保存一份
			// if(this.currentSwiperIndex == 0) this.oldFeedsScrollTop = obj.scrollTop
			// else this.oldNewsScrollTop = obj.scrollTop
			// // console.log(obj.scrollTop);
			// //控制导航栏的显示，隐藏
			// if(obj.scrollTop > 200) this.navBarShowTag = true
			// else this.navBarShowTag = false
		},
		methods: {
			// 用于把当前点击图片的信息 保存到仓库中，提供给 详情页面
			ab(a){
				let jsdata = JSON.stringify(a)
				this.$store.dispatch('login',jsdata)
			},
			// tab 链接跳转
			gotoTab(url) {
				// uni.switchTab:跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
				uni.switchTab({
					url: url
				})
			},
			// 推荐，资讯点击切换效率。
			swiperChange(index) {
				if(index == 0){
					this.swiperSliderHeight = this.swiperSliderFeedsHeight
					uni.pageScrollTo({
						duration:0,
						scrollTop:this.oldFeedsScrollTop
					})
				}
				else{
					this.swiperSliderHeight = this.swiperSliderNewsHeight
					uni.pageScrollTo({
						duration:0,
						scrollTop:this.oldNewsScrollTop
					})
				}
				this.currentSwiperIndex = index
			},
			//推荐，资讯滑动切换效率。（组件自带事件）
			swiperSlider(event){
				let index = event.detail.current
				if(index == 0) {
					this.swiperSliderHeight = this.swiperSliderFeedsHeight
					uni.pageScrollTo({
						duration:0,
						scrollTop:this.oldFeedsScrollTop
					})
				}
				else{
					this.swiperSliderHeight = this.swiperSliderNewsHeight
					uni.pageScrollTo({
						duration:0,
						scrollTop:this.oldNewsScrollTop
					})
				}
				// console.log('event：',event);
			
				this.currentSwiperIndex = index
			},
			//【发送请求】 获取推荐，资讯的数据
			async adver() {
				//发送请求
				let adverr = await this.$u.api.getAdvert();
				// 提取推荐页面数据
				let feedsList = adverr.recommend.map(item => {
					return item
				})
				// 把原来的数据和新获取的数据重新赋值给 feedsList，
				this.feedsList = [...this.feedsList,...feedsList]
				// 提取资讯列表数据
				this.newsList = adverr.information.map(item => {
					return item
				})
				//
				uni.$on("swiperHeightChang",height => {
					console.log('瀑布流计算出来的轮播器高度：',height);
					this.swiperSliderHeight = height
				 	              // 下面两个作用实现动态切换
					// 推荐轮播器高度
					this.swiperSliderFeedsHeight = height
					// 资讯轮播器高度
					this.swiperSliderNewsHeight = (this.newsList.length * 95+20) + 'px'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	#sns {
		background-color: #f1f1f1;
	}

	// 推荐、咨询 按钮样式
	.tabs-box {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 750upx;

		.one-nav {
			width: 120upx;
			color: #9B9B9B;
			font-size: 36upx;
			text-align: center;
			font-weight: blod;

			&.nav-actived {
				color: #0050FF;
			}
		}
	}

	.header-box {
		position: relative;
		left: 0;
		height: 500upx;
		background-color: #f1f1f1;
		z-index: 1;

		// 广告位轮播器相关样式
		.swiper {
			width: 750upx;
			height: 400upx;
			position: absolute;
			left: 0;
			top: 0;
			text-align: center;
			z-index: 1;

			.banner-swiper-img {
				width: 750upx;
				height: 400upx;
				box-shadow: 0 0 2px 0 rgb(188, 188, 188);
			}
		}

		.crile {
			width: 750upx;
			height: 50upx;
			position: absolute;
			left: 0;
			top: 355upx;
			z-index: 9;
		}

		// 新鲜事 活动墙 按钮样式
		.card-header {
			position: absolute;
			left: 0;
			top: 320upx;
			height: 160upx;
			z-index: 99;
			width: 710upx;
			margin-left: 20upx;
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: space-between;
			align-items: center;
			align-content: center;

			.card-one {
				// width: 328upx;
				width: 308upx;  //需要设置小一点
				height: 96upx;
				border-radius: 49upx;
				background-color: #fff;
				margin: 0 10upx;
				box-shadow: 0 0 2px 0 rgb(188, 188, 188);
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				justify-content: flex-start;
				align-items: center;
				align-content: center;

				.img {
					width: 44upx;
					height: 44upx;
					margin-left: 50upx;
				}

				.iright {
					margin-left: 30upx;
					text-align: left;
					color: #888;

					.title {
						font-size: 30upx;
						color: #001432;
					}

					.iview {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						justify-content: space-between;
						align-items: center;
						align-content: center;
						font-size: 20upx;
						margin-top: -5upx;
					}
				}
			}
		}

		// 推荐、咨询 按钮样式
		.tabs-box {
			width: 750upx;
			position: absolute;
			z-index: 1;
			left: 0;
			top: 480upx;
			display: flex;
			flex-direction: row;
			justify-content: center;

			.one-nav {
				height: 80upx;
				width: 110upx;
				color: #9B9B9B;
				font-size: 36upx;
				text-align: center;
				font-weight: blod;

				&.nav-actived {
					color: #0050FF;
				}
			}
		}
	}

	// 此刻 栏目样式\
	.swiper-box {
		background-color: #f1f1f1;
		padding: 60upx 0 40upx;
	}

	.sns-now {

		// 动态相关瀑布流样式
		.feeds-box {
			width: 735upx;
			margin-left: 13upx;
			padding-bottom: 20upx;

			.feed-one {
				width: 358upx;
				margin-bottom: 12upx;
				background-color: #FFF;
				border-radius: 20upx;

				position: relative;

				.feed-img {
					width: 358upx;
					height: 300upx;
					border-radius: 10upx;
				}

				.feed-title {
					width: 350upx;
					margin-top: 15upx;
					margin-left: 10upx;
					font-size: 28upx;
					line-height: 40upx;
					color: #001432;
					text-align: left;
				}

				.feed-info {
					display: flex;
					flex-direction: row;
					flex-wrap: nowrap;
					justify-content: space-between;
					align-items: center;
					align-content: center;
					margin-top: 10upx;
					font-size: 20upx;
					color: #666;
					padding: 0 10upx 16upx;

					.iview {
						display: flex;
						flex-direction: row;
						flex-wrap: nowrap;
						justify-content: space-between;
						align-items: center;
						align-content: center;

						.ilike {
							display: flex;
							flex-direction: row;
							flex-wrap: nowrap;
							justify-content: space-between;
							align-items: center;
							align-content: center;
							height: 60upx;
							padding: 0 10upx;
							background-color: #FFFFFF;
						}
					}

					.avatar {
						margin-right: 10upx;
						height: 40upx;
						width: 40upx;
						border-radius: 50%;
						border: 1upx solid #eee;
					}

					.name {
						max-width: 120upx;
						color: #757474;
					}

					.micon {
						width: 32upx;
						height: 28upx;
					}

					.love-count {
						padding-left: 10upx;
						color: #757474;
					}
				}
			}
		}
	}

	// 轮播页面 资讯
	.sns-news {
		background-color: #fff;
		width: 750upx;

		.one-new {
			width: 700upx;
			height: 74px;
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: space-around;
			align-items: flex-start;
			align-content: center;
			padding-bottom: 10px;
			padding-top: 10px;
			padding-left: 25upx;
			padding-right: 25upx;
			border-bottom: 1px solid #f1f1f1;

			.left {
				width: 490upx;
				height: 140upx;
				text-align: left;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: flex-start;

				.title {
					font-size: 30upx;
					line-height: 42upx;
					color: #001432;
					margin-top: 21upx;
				}

				.uinfo {
					width: 490upx;
					display: flex;
					flex-direction: row;
					flex-wrap: nowrap;
					justify-content: space-between;
					align-items: center;
					align-content: center;
					margin-top: 6upx;
					font-size: 20upx;
					color: #999;

					.utime {
						font-size: 24upx;

						.name {
							max-width: 120upx;
							color: #777;
						}
					}
				}
			}

			.right {
				width: 120upx;

				.pic {
					width: 120upx;
					height: 120upx;
					margin-top: 20upx;
					border-radius: 6upx;
				}
			}
		}
	}
</style>
